Karura

Row

Overall Total Value Locked (TVL)

  • The Overall TVL in the table below does not includes the Crowdloan TVL, which also seems to be the case on the stats.Karura.network page.
Date Block dexTVL bridgeTVL liquidStakingTVL crowdloanTVL stableCoinTVL OverallTVL
2021-12-30 1,216,792 28,477,809 51,134,081 0 140,764,558 30,569,447 110,181,338
2021-12-31 1,223,729 27,960,331 48,351,235 0 139,511,714 28,130,859 104,442,425
2022-01-01 1,230,690 28,936,720 49,930,895 0 143,766,372 28,862,133 107,729,748
2022-01-02 1,237,666 28,994,199 51,315,701 0 147,915,792 29,765,768 110,075,668
2022-01-03 1,244,636 28,394,734 50,409,234 0 145,229,694 28,808,841 107,612,808
2022-01-04 1,251,539 28,279,294 52,019,213 0 150,381,390 19,511,557 99,810,065
2022-01-05 1,258,602 27,314,933 46,040,367 17,723,365 132,711,276 25,764,959 116,843,624
2022-01-06 1,265,574 26,758,257 46,719,824 17,710,566 135,913,545 26,374,687 117,563,336
2022-01-07 1,272,463 26,061,882 43,540,840 16,477,194 127,063,454 24,655,919 110,735,836
2022-01-08 1,279,512 24,834,724 41,534,446 15,777,941 120,914,495 23,320,806 105,467,917
2022-01-09 1,286,513 24,276,416 42,503,173 16,224,592 123,710,843 23,863,867 106,868,048
2022-01-10 1,293,438 23,718,700 41,406,493 15,865,354 120,252,993 23,289,506 104,280,054
2022-01-11 1,300,364 24,179,221 42,245,489 14,949,413 130,130,417 23,500,400 104,874,523
2022-01-12 1,307,287 25,224,845 44,761,105 16,031,649 138,263,881 24,877,177 110,894,775
2022-01-13 1,314,250 24,947,366 43,690,075 15,588,045 134,655,690 24,798,264 109,023,750
2022-01-14 1,321,145 25,197,576 45,510,863 16,310,065 140,874,808 25,251,168 112,269,673
2022-01-15 1,327,988 25,173,731 46,088,047 16,610,581 143,741,316 25,720,836 113,593,195
2022-01-16 1,334,836 24,928,016 44,275,915 15,933,003 138,649,757 24,757,053 109,893,987
2022-01-17 1,341,616 24,961,950 41,214,742 14,793,150 128,932,698 22,990,965 103,960,807
2022-01-18 1,348,556 24,818,429 43,032,685 14,731,644 127,815,161 22,932,495 105,515,253
2022-01-19 1,355,482 24,389,176 41,915,120 14,189,131 123,004,239 22,172,875 102,666,303
2022-01-20 1,362,368 24,053,310 39,443,590 14,140,066 122,392,851 22,542,280 100,179,247
2022-01-21 1,369,220 20,551,211 33,823,202 12,780,374 107,393,801 20,114,175 87,268,962
2022-01-22 1,376,066 16,317,883 29,174,017 11,865,142 94,439,392 17,473,724 74,830,765
2022-01-23 1,382,988 16,849,199 29,630,981 11,980,949 94,935,519 17,867,771 76,328,900
2022-01-24 1,389,889 14,899,662 28,059,017 11,315,066 87,298,181 16,469,949 70,743,693
2022-01-25 1,396,806 17,827,278 27,351,713 11,103,209 86,461,281 15,477,409 71,759,608
2022-01-26 1,403,696 16,977,302 25,785,350 10,568,643 82,532,362 14,509,393 67,840,688
2022-01-27 1,410,563 16,130,280 25,036,579 10,573,141 81,043,983 14,142,683 65,882,684
2022-01-28 1,417,498 16,380,471 26,320,983 11,004,982 84,592,037 14,880,991 68,587,427
2022-01-29 1,424,433 16,530,233 26,492,598 11,160,147 85,784,745 15,030,737 69,213,716
2022-01-30 1,431,336 16,734,485 25,388,669 10,700,703 82,116,417 14,737,654 67,561,510
2022-01-31 1,438,201 16,430,659 25,336,333 10,668,834 82,341,929 14,557,891 66,993,717
2022-02-01 1,445,158 16,888,402 26,503,043 11,034,229 86,486,338 15,404,398 69,830,073
2022-02-02 1,452,085 16,385,550 24,721,945 10,349,332 81,214,369 14,365,291 65,822,119
2022-02-03 1,459,034 16,206,473 24,517,673 10,278,882 80,562,891 14,217,673 65,220,701
2022-02-04 1,465,910 17,573,369 27,001,352 11,391,759 89,282,686 15,827,834 71,794,313
2022-02-05 1,472,341 18,115,769 28,015,169 11,849,538 92,805,684 16,398,245 74,378,720
2022-02-06 1,478,495 18,433,454 29,160,462 12,416,470 97,155,559 16,889,128 76,899,514
2022-02-07 1,484,572 18,710,293 29,765,567 12,616,948 99,906,804 17,157,854 78,250,662
2022-02-08 1,489,375 18,120,633 28,506,170 12,049,255 95,491,781 16,186,674 74,862,732
2022-02-09 1,493,799 18,398,595 29,062,161 12,480,553 97,165,581 16,525,057 76,466,366
2022-02-10 1,498,135 18,252,209 29,010,769 13,414,151 90,680,860 16,628,704 77,305,833
2022-02-11 1,502,412 17,183,038 26,195,867 12,137,702 81,730,541 15,073,577 70,590,183
2022-02-12 1,506,675 16,523,389 26,215,714 12,182,443 83,118,693 15,213,651 70,135,197
2022-02-13 1,510,965 16,142,836 26,277,397 12,321,765 82,176,554 15,517,512 70,259,509
2022-02-14 1,515,339 15,758,810 26,685,605 12,471,674 83,664,933 15,557,219 70,473,307
2022-02-15 1,519,779 16,270,303 27,702,226 13,000,873 87,273,124 16,107,445 73,080,848
2022-02-16 1,524,208 16,060,289 26,446,000 12,573,172 84,316,412 15,537,487 70,616,948
2022-02-17 1,528,534 15,144,082 24,566,632 11,718,180 78,222,578 14,190,604 65,619,498
2022-02-18 1,532,831 14,556,699 23,011,996 11,201,867 74,428,966 13,518,494 62,289,056
2022-02-19 1,537,082 14,233,537 23,118,857 11,320,339 74,905,046 13,445,019 62,117,753
2022-02-20 1,541,290 13,366,110 21,939,745 10,655,435 70,229,432 12,490,299 58,451,589
2022-02-21 1,545,293 12,628,156 20,199,111 9,792,882 64,326,030 12,830,700 55,450,849
2022-02-22 1,549,212 11,534,618 20,187,132 9,860,940 64,862,248 12,685,919 54,268,609
2022-02-23 1,553,133 11,597,913 19,374,788 9,445,214 62,075,922 12,841,973 53,259,887
2022-02-24 1,557,141 10,531,902 18,125,564 8,915,579 58,402,583 12,117,829 49,690,874
2022-02-25 1,561,208 10,855,034 19,479,446 9,614,395 62,887,765 13,064,856 53,013,730
2022-02-26 1,565,186 10,921,496 19,745,946 9,770,904 64,185,712 13,078,500 53,516,847
2022-02-27 1,569,198 10,116,662 18,021,713 8,984,982 58,898,709 12,057,255 49,180,613
2022-02-28 1,573,192 10,479,921 19,541,265 9,695,624 64,506,440 13,051,277 52,768,086
2022-03-01 1,577,360 10,879,012 20,850,365 10,451,229 69,793,442 14,094,592 56,275,198
2022-03-02 1,581,658 10,743,363 20,774,682 10,443,673 69,763,374 13,957,452 55,919,170
2022-03-03 1,586,008 10,431,306 19,607,462 9,884,753 65,944,705 13,403,247 53,326,768
2022-03-04 1,590,339 9,792,560 18,027,834 9,086,777 60,311,918 12,258,298 49,165,469
2022-03-05 1,594,558 9,823,524 18,479,128 9,309,293 61,619,887 12,338,275 49,950,220
2022-03-06 1,598,920 9,881,613 18,457,052 0 61,685,035 12,547,580 40,886,246
2022-03-07 1,603,233 9,684,250 17,869,433 9,069,358 59,986,178 11,954,912 48,577,952
2022-03-08 1,607,573 9,699,877 18,491,095 9,387,102 60,737,885 12,157,986 49,736,060
2022-03-09 1,611,910 10,236,962 20,338,510 10,343,541 67,117,367 13,544,592 54,463,605
2022-03-10 1,616,357 9,900,504 18,862,832 9,634,859 62,842,663 12,491,199 50,889,394
2022-03-11 1,620,876 9,938,713 19,234,455 9,726,906 62,496,878 12,326,243 51,226,317
2022-03-12 1,625,432 9,937,874 19,040,078 9,895,685 62,301,434 8,778,466 47,652,102
2022-03-13 1,629,884 9,582,229 18,671,395 9,700,675 61,218,977 11,976,980 49,931,279
2022-03-14 1,634,405 9,517,275 18,337,095 9,596,841 59,835,837 11,662,586 49,113,796
2022-03-15 1,638,930 9,610,346 19,069,154 9,986,903 62,476,832 12,033,881 50,700,284
2022-03-16 1,643,457 9,916,992 19,822,002 10,344,356 64,857,236 12,611,340 52,694,690
2022-03-17 1,647,983 10,164,252 20,476,914 10,758,928 67,488,209 13,177,318 54,577,412
2022-03-18 1,652,652 10,723,778 22,411,036 11,811,109 74,028,055 14,620,780 59,566,704
2022-03-19 1,657,396 10,857,718 22,337,744 11,760,488 73,587,054 14,630,661 59,586,611
2022-03-20 1,662,219 10,805,099 22,714,583 12,004,244 75,235,797 14,435,960 59,959,887
2022-03-21 1,666,997 10,821,370 23,115,687 12,289,184 77,490,917 14,920,932 61,147,173
2022-03-22 1,671,509 10,996,672 23,591,888 12,586,513 79,074,512 15,076,643 62,251,716
2022-03-23 1,675,850 11,233,620 24,251,395 13,062,818 82,221,656 15,709,358 64,257,191
2022-03-24 1,680,082 11,210,552 24,245,846 13,021,250 81,961,065 15,926,435 64,404,082
2022-03-25 1,684,322 11,178,468 24,391,600 13,123,045 82,517,327 15,906,943 64,600,056
2022-03-26 1,688,574 11,229,645 24,181,495 13,049,274 82,607,532 15,887,907 64,348,320
2022-03-27 1,692,713 12,008,614 25,634,020 13,815,147 87,157,862 17,345,787 68,803,568
2022-03-28 1,696,873 11,865,913 25,333,349 13,634,351 86,130,530 17,079,663 67,913,276
2022-03-29 1,700,908 12,430,365 26,708,554 14,389,203 91,031,656 17,897,927 71,426,048
2022-03-30 1,704,922 12,650,038 27,939,922 15,121,153 94,504,540 18,441,788 74,152,900
2022-03-31 1,708,906 12,971,828 28,174,539 15,209,696 94,975,610 19,092,108 75,448,171
2022-04-01 1,712,970 12,900,127 27,991,848 15,225,084 95,055,792 18,957,801 75,074,860
2022-04-02 1,717,078 13,877,724 30,155,510 16,411,345 102,197,003 20,648,204 81,092,783
2022-04-03 1,721,276 13,765,513 29,504,333 16,147,422 100,488,124 20,101,758 79,519,026
2022-04-04 1,725,277 13,113,962 27,491,702 14,834,897 94,544,631 18,544,418 73,984,978
2022-04-05 1,729,249 12,982,728 26,535,729 14,361,013 91,056,713 18,129,594 72,009,064
2022-04-06 1,733,105 12,388,690 25,344,855 13,757,432 86,887,248 17,126,109 68,617,086
2022-04-07 1,736,920 12,192,261 26,540,457 14,445,681 90,901,360 19,099,672 72,278,070
2022-04-08 1,740,728 11,748,502 24,935,815 13,690,735 86,235,769 17,863,594 68,238,646
2022-04-09 1,744,511 11,679,970 25,748,801 14,129,747 89,012,072 17,970,468 69,528,987
2022-04-10 1,748,342 11,992,406 24,964,446 13,629,715 86,250,803 18,709,034 69,295,601
2022-04-11 1,752,148 10,613,556 22,436,858 12,131,417 76,819,392 15,794,129 60,975,960
2022-04-12 1,756,009 10,807,351 22,302,185 12,208,892 76,919,620 15,777,556 61,095,983
2022-04-13 1,759,810 11,088,597 23,285,121 12,792,713 80,051,730 16,804,316 63,970,748
2022-04-14 1,763,619 11,029,818 23,483,695 12,946,382 80,337,379 16,752,968 64,212,863
2022-04-15 1,767,375 11,234,948 24,296,852 13,410,938 82,893,181 17,192,776 66,135,515
2022-04-16 1,771,087 11,528,819 25,595,972 14,131,118 87,253,078 18,011,711 69,267,619
2022-04-17 1,774,902 11,432,100 25,937,671 13,960,079 86,220,735 18,250,198 69,580,048
2022-04-18 1,778,708 11,255,844 24,795,150 13,882,331 83,644,887 16,554,502 66,487,826
2022-04-19 1,782,472 11,267,515 26,321,766 14,713,377 88,576,082 17,592,326 69,894,984
2022-04-20 1,786,343 11,377,126 26,774,582 15,042,279 90,385,189 18,213,130 71,407,118
2022-04-21 1,790,294 11,532,164 25,357,311 14,142,810 83,995,684 18,110,831 69,143,116
2022-04-22 1,794,197 11,731,877 25,056,511 14,117,181 82,407,077 18,008,727 68,914,296
2022-04-23 1,798,162 12,038,829 25,712,654 15,293,481 82,231,679 19,281,727 72,326,691
2022-04-24 1,802,115 11,786,301 24,888,665 14,807,685 80,217,106 19,199,526 70,682,177
2022-04-25 1,806,087 11,986,409 25,468,757 15,201,526 81,966,076 19,705,764 72,362,456
2022-04-26 1,810,046 11,595,769 23,959,849 14,400,527 77,330,553 18,674,336 68,630,481
2022-04-27 1,814,021 11,456,731 24,339,755 14,702,543 78,793,875 18,695,554 69,194,584
2022-04-28 1,817,974 11,458,940 24,219,751 14,663,300 78,598,431 18,558,400 68,900,391
2022-04-29 1,821,980 10,877,984 22,316,905 13,414,234 73,110,973 17,325,218 63,934,340
2022-04-30 1,826,039 10,135,613 20,044,019 12,055,765 65,568,852 15,469,289 57,704,686
2022-05-01 1,830,125 10,191,535 20,435,514 12,283,977 66,691,400 15,730,769 58,641,796
2022-05-02 1,834,044 10,045,055 20,250,105 12,229,399 65,513,727 15,789,105 58,313,665
2022-05-03 1,837,991 9,940,595 19,795,543 11,951,408 63,910,086 15,530,323 57,217,868
2022-05-04 1,841,986 10,332,852 21,868,892 13,203,540 70,359,728 16,898,031 62,303,315
2022-05-05 1,846,132 9,849,962 19,862,878 12,007,333 63,824,893 15,858,555 57,578,728
2022-05-06 1,850,190 9,840,823 20,011,409 12,050,273 63,484,119 15,831,354 57,733,858
2022-05-07 1,854,421 9,415,404 18,292,304 11,169,970 58,848,596 14,431,136 53,308,813
2022-05-08 1,858,658 8,632,921 17,530,427 10,679,637 56,127,418 13,330,527 50,173,513
2022-05-09 1,862,992 7,868,856 15,385,350 9,370,326 49,256,821 11,702,787 44,327,319
2022-05-10 1,867,957 7,063,847 14,780,904 8,978,157 46,465,484 11,443,966 42,266,874
2022-05-11 1,873,972 5,668,544 11,500,875 7,294,151 37,138,310 10,608,911 35,072,481
2022-05-12 1,880,188 5,278,034 11,488,206 7,527,435 34,763,920 9,753,341 34,047,016
2022-05-13 1,886,554 5,513,923 12,083,173 7,974,383 37,378,856 10,508,482 36,079,961
2022-05-14 1,892,961 4,615,804 12,049,192 7,979,481 36,963,914 10,145,673 34,790,150
2022-05-15 1,899,371 5,098,244 13,985,476 9,704,209 38,612,156 12,011,210 40,799,139
2022-05-16 1,905,778 5,015,528 15,746,193 11,189,975 36,873,709 13,368,708 45,320,404
2022-05-17 1,912,230 5,443,403 17,728,919 12,839,946 41,591,419 14,786,902 50,799,170
2022-05-18 1,918,591 4,831,627 16,057,875 11,633,620 37,997,260 13,547,934 46,071,056
2022-05-19 1,924,950 5,051,995 16,446,611 11,866,785 39,895,569 13,862,470 47,227,861
2022-05-20 1,931,289 4,898,759 15,865,270 11,314,258 37,884,504 13,279,427 45,357,714
2022-05-21 1,937,640 5,106,622 16,983,056 12,029,939 40,106,047 14,171,588 48,291,205
2022-05-22 1,944,002 5,212,005 17,143,997 12,141,015 40,352,106 14,419,157 48,916,175
2022-05-23 1,950,438 5,142,609 17,651,424 12,545,228 41,399,483 14,829,710 50,168,971
2022-05-24 1,956,833 5,057,268 17,671,288 12,518,757 41,168,960 14,829,440 50,076,752
2022-05-25 1,963,028 5,050,561 17,485,105 12,427,987 40,146,639 14,681,528 49,645,181
2022-05-26 1,969,604 4,618,061 15,453,989 11,036,697 35,264,055 13,114,722 44,223,469
2022-05-27 1,975,998 4,450,433 14,713,121 10,552,867 33,481,007 12,570,318 42,286,739
2022-05-28 1,982,535 4,453,273 14,970,894 10,776,720 33,908,979 12,802,796 43,003,684
2022-05-29 1,988,972 4,492,985 15,845,697 11,415,697 35,866,422 13,441,785 45,196,163
2022-05-30 1,995,443 4,740,857 17,238,125 12,429,566 38,914,342 14,734,171 49,142,719
2022-05-31 2,001,782 4,882,592 17,709,040 12,660,720 39,629,966 15,254,737 50,507,089
2022-06-01 2,008,273 4,595,685 16,885,931 12,175,530 36,748,425 14,597,291 48,254,437
2022-06-02 2,014,698 4,575,532 17,047,748 12,280,079 37,009,016 14,688,565 48,591,925
2022-06-03 2,021,059 4,465,668 15,777,245 11,427,459 34,174,081 13,728,036 45,398,407
2022-06-04 2,027,513 4,449,137 15,966,827 11,565,414 34,565,469 13,811,670 45,793,047
2022-06-05 2,034,053 4,407,153 15,904,103 11,536,661 34,268,796 13,862,453 45,710,370
2022-06-06 2,040,490 4,474,010 16,394,097 11,910,789 35,089,659 14,227,562 47,006,457
2022-06-07 2,046,816 4,388,449 16,064,594 11,761,642 33,590,756 14,005,969 46,220,654
2022-06-08 2,053,305 4,306,263 15,963,129 11,707,615 33,359,732 13,905,651 45,882,658
2022-06-09 2,059,744 4,359,234 15,920,907 11,686,991 33,250,484 14,076,099 46,043,231
2022-06-10 2,066,092 4,117,787 14,254,747 10,449,312 29,651,814 12,632,440 41,454,286
2022-06-11 2,072,325 3,963,451 13,902,587 10,149,406 28,573,867 12,316,369 40,331,813
2022-06-12 2,078,699 3,771,603 12,642,038 9,236,596 26,037,108 11,367,864 37,018,101
2022-06-13 2,084,974 3,372,037 11,933,785 7,831,543 23,066,865 9,703,009 32,840,374
2022-06-14 2,091,245 3,589,270 13,248,299 9,476,166 25,683,305 10,736,969 37,050,704
2022-06-15 2,097,549 3,764,179 14,852,362 10,777,366 28,624,983 12,009,355 41,403,262
2022-06-16 2,103,874 3,440,450 12,386,780 8,915,974 24,452,010 10,170,473 34,913,677
2022-06-17 2,110,292 3,409,689 12,465,328 8,991,893 24,550,734 10,237,658 35,104,568
2022-06-18 2,116,724 3,445,679 12,352,682 8,906,555 24,393,878 10,142,229 34,847,145
2022-06-19 2,123,104 3,549,822 12,796,912 9,213,173 25,211,735 10,658,611 36,218,518
2022-06-20 2,129,497 3,622,729 13,707,560 9,626,126 26,363,349 11,062,858 38,019,274
2022-06-21 2,135,922 3,727,008 13,680,433 9,799,394 26,940,660 11,270,679 38,477,515
2022-06-22 2,142,278 3,634,993 13,364,629 9,604,004 26,269,636 10,997,083 37,600,709
2022-06-23 2,148,575 3,646,287 14,087,142 9,918,909 27,162,162 11,193,642 38,845,980
2022-06-24 2,155,139 3,752,477 14,978,106 10,567,805 28,917,147 11,840,803 41,139,191
2022-06-25 2,161,562 3,759,708 14,893,419 10,473,270 28,632,500 11,689,145 40,815,542
2022-06-26 2,166,585 3,904,549 13,618,871 9,662,178 27,537,515 11,543,893 38,729,491

Row

Components of Total Value Locked

Row

Sources

Please click on the Source Code link at the top of the document to see the full source code. Here is a summary:

dexTVL

  • sum of tvlUSD from the xx function in the subscanr package for R. You can also use the Karura-dex project hosted on Subquery Network with this query:
{ 
  dailyPools { 
    nodes { 
      timestamp token0 {id} token1 {id} feeRateUSD dailyTradeVolumeUSD totalTVL txCount updateAtBlock {id}  
    } 
  } 
}

BridgeTVL

  • KSM (or DOT) obtained from the py-substrate-interface using function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘Token’: ‘KSM’}], block_hash = hash)
  • LKSM (or LDOT) obtained from function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘Token’: ‘LKSM’}], block_hash = hash)
  • both mulitplied by KSM (or DOT) price for the specific block number

liquidStakingTVL

  • KSM (or DOT) obtained from the py-substrate-interface using function substrate.query(module=‘Homa’,storage_function=‘TotalStakingBonded’, block_hash = hash)
  • mulitplied by KSM (or DOT) price for the specific block number

lcdotTVL (Acala only)

  • LCDOT obtained from the py-substrate-interface using function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘LiquidCrowdloan’: 13}], block_hash = hash)
  • mulitplied by DOT price for the specific block number

stableCoinTVL

{ 
  dailyCollaterals { 
    nodes { 
      collateral {id} depositAmount debitAmount depositVolumeUSD debitVolumeUSD
      depositChangedUSD debitChangedUSD debitExchangeRate timestamp txCount 
    } 
  } 
}
---
title: "Acala / Karura TVL Dashboard"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
params:
  network: Karura
  window: 7
  
---

```{css custom1, echo=FALSE}
.dataTables_scrollBody {
    max-height: 100% !important;
}
```

```{r global, include=FALSE}
library(knitr)
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

library(ggplot2)
# library(dygraphs)
library(kableExtra)
library(formattable)
library(lubridate)
library(flexdashboard)
library(DT)
library(subscanr)
library(formattable)
library(ghql)
x <- GraphqlClient$new()

# Helper function to concat
`%+%` <- function(a, b) paste0(a, b)

# window = params$window
window <- today() - as.Date("2021-12-31") + 1
network = params$network

# Liquidity Pool TVL ($3.72M) = totalDefiTvl = totalDexTvl + totalDexLockedTvl + homaLocked + stableCoinTvl
dex <- getDailyPools_acala_dex(network, window) %>%
  setorder(Date, pair)

dailyTVL <- dex[, .(max(updateAtBlock.id), sum(tvlUSD)), by = Date] %>%
  setnames(c("V1","V2"), c("Block","dexTVL"))

library(reticulate)
# use_python("/opt/homebrew/bin/python3.9")
```

```{python, include=FALSE}
from substrateinterface import SubstrateInterface

def getTotalIssuanceKSM():
    url = 'wss://karura.polkawallet.io'
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
    ksm = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'KSM'}], block_hash = hash)
    lksm = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LKSM'}], block_hash = hash)
    result = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'KSM'}], block_hash = hash)
    price = result.value['value'] / 10**18
    
    # homa = substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)
    try:
      homa = substrate.query_map(module='Homa',storage_function='StakingLedgers', block_hash = hash)
      amount = 0
      for res in homa:
          amount += res[1].value['bonded']
    except Exception as e:
      amount = 0
      
    bridge = (float(ksm.value / 10**12) + float(lksm.value / 10**12 / 8.5)) * price
    liquidKSM = float(amount / 10**12) * price
    crowdloanTVL = (501137661910050505 / 10**12) * price
    return [bridge, liquidKSM, crowdloanTVL, block_id]

def getTotalIssuanceDOT():
    url = 'wss://acala-rpc-0.aca-api.network'
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
    dot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'DOT'}], block_hash = hash)
    ldot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LDOT'}], block_hash = hash)
    lcdot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'LiquidCrowdloan': 13}], block_hash = hash)
    result = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'DOT'}], block_hash = hash)
    price = result.value['value'] / 10**18
    
    # homa = substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)
    homa = substrate.query_map(module='Homa',storage_function='StakingLedgers', block_hash = hash)
    amount = 0
    for res in homa:
        amount += res[1].value['bonded']
  
    bridge = (float(dot.value / 10**10) + float(ldot.value / 10**10 / 10)) * price
    liquidDOT = float(amount / 10**10) * price
    lcDOT = float(lcdot.value / 10**10) * price
    crowdloanTVL = (325159802323576263 / 10**10) * price
    return [bridge, liquidDOT, crowdloanTVL, lcDOT, block_id]

```



```{r tvl, cache = TRUE, include=FALSE}

# dailyTVL[, M := month(Date) %+% year(Date)]
# dailyTVL[, maxDate := max(Date), by = M]
# dailyTVL <- dailyTVL[Date == maxDate]

# LP Token Staking TVL = totalDexTvl = useTotalDexPoolTVL()
# totalDexLockedTvl = useTotalStaking() = api.query.dex?.liquidityPool
# KSM Bridge TVL = totalIssuanceKSM = useTotalIssuance('KSM') = api.query.tokens?.totalIssuance
dailyTVL[, bridgeTVL := 0]
# Liquid KSM TVL = totalLocked in Homa = useState()
dailyTVL[, liquidStakingTVL := 0]
# homeLocked = totalStaking * price
dailyTVL[, crowdloanTVL := 0]
if (tolower(network) == "acala") dailyTVL[, lcdotTVL := 0]

for (i in 1:nrow(dailyTVL)) {
  py_run_string("block_id = " %+% dailyTVL$Block[i])
  if (tolower(network) == "karura") {
    tmp = py$getTotalIssuanceKSM()
  } else {
    tmp = py$getTotalIssuanceDOT()
    dailyTVL$lcdotTVL[i] <- tmp[4]
  }
  dailyTVL$bridgeTVL[i] <- tmp[1]
  dailyTVL$liquidStakingTVL[i] <- tmp[2]
  dailyTVL$crowdloanTVL[i] <- tmp[3]
}

# stableCoinTvl
# const stableCoinTVL = useTotalLocked(); = api.query.loans?.totalPositions
collateral <- getLoansDailyCollateral_acala_loan(network, window, staging = FALSE) %>%
  setorder(Date, collateral.id)
stableCoinTvl <- collateral[, sum(depositVolumeUSD), by = Date] %>%
  setnames("V1", "stableCoinTVL")
dailyTVL <- merge(dailyTVL, stableCoinTvl, by = "Date", all.x = TRUE)

# If monthly, remove these fields
# try(dailyTVL[, M := NULL])
# try(dailyTVL[, maxDate := NULL])

# Make columns numeric
for (col in names(dailyTVL)[-1]) set(dailyTVL, j=col, value=as.numeric(dailyTVL[[col]])) 
if (tolower(network) == "acala") {
  dailyTVL[, OverallTVL := dexTVL + bridgeTVL + liquidStakingTVL + lcdotTVL + stableCoinTVL]
} else {
  dailyTVL[, OverallTVL := dexTVL + bridgeTVL + liquidStakingTVL + stableCoinTVL]
}

```

# `r network` {.tabset}

Row
----

### Overall Total Value Locked (TVL)

* The Overall TVL in the table below does not includes the Crowdloan TVL, which also seems to be the case on the stats.`r network`.network page.

```{r k_tvl}

knitr::kable(dailyTVL, escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

```

Row
----

### Components of Total Value Locked

```{r k_plot1}

if (tolower(network) == "acala") {
  dat <- melt(data = dailyTVL[, .(Date, dexTVL, bridgeTVL, liquidStakingTVL, lcdotTVL, stableCoinTVL)], 
            id.vars = "Date",
            value.name = "TVL")
  cols <- c('dexTVL', 'bridgeTVL', 'liquidStakingTVL', 'LCDOTTVL', 'stableCoinTVL')
} else {
  dat <- melt(data = dailyTVL[, .(Date, dexTVL, bridgeTVL, liquidStakingTVL, stableCoinTVL)], 
            id.vars = "Date",
            value.name = "TVL")
  cols <- c('dexTVL', 'bridgeTVL', 'liquidStakingTVL', 'stableCoinTVL')
  
}
dat[, TVL := TVL / 1e6]
 
# Stacked
ggplot(dat, aes(fill=variable, y=TVL, x=Date)) + 
    geom_bar(position="stack", stat="identity") +
    ggtitle(label="Components of Total Value Locked") + 
    ylab("Totval Value Locked (in millions USD)")

```


Row
----

### Sources

Please click on the *Source Code* link at the top of the document to see the full source code.  Here is a summary:

#### dexTVL 

- sum of tvlUSD from the xx function in the [subscanr](https://github.com/rogerjbos/subscanr) package for R.  You can also use the [Karura-dex](https://explorer.subquery.network/subquery/AcalaNetwork/karura-dex) project hosted on Subquery Network with this query:
```
{ 
  dailyPools { 
    nodes { 
      timestamp token0 {id} token1 {id} feeRateUSD dailyTradeVolumeUSD totalTVL txCount updateAtBlock {id}  
    } 
  } 
}
```

#### BridgeTVL
- KSM (or DOT) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'KSM'}], block_hash = hash)*
- LKSM (or LDOT) obtained from function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LKSM'}], block_hash = hash)*
- both mulitplied by KSM (or DOT) price for the specific block number

#### liquidStakingTVL
- KSM (or DOT) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)*
- mulitplied by KSM (or DOT) price for the specific block number

#### lcdotTVL (Acala only)
- LCDOT obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'LiquidCrowdloan': 13}], block_hash = hash)*
- mulitplied by DOT price for the specific block number

#### stableCoinTVL
- sum of depositVolumeUSD from the *getLoansDailyCollateral_acala_loan* function in the [subscanr](https://github.com/rogerjbos/subscanr) package for R.  You can also use the [Karura-loan]https://explorer.subquery.network/subquery/AcalaNetwork/karura-loan) project hosted on Subquery Network with this query:
```
{ 
  dailyCollaterals { 
    nodes { 
      collateral {id} depositAmount debitAmount depositVolumeUSD debitVolumeUSD
      depositChangedUSD debitChangedUSD debitExchangeRate timestamp txCount 
    } 
  } 
}
```